package com.youbo.cloudmall.product.service.impl;

import com.youbo.cloudmall.product.dao.AttrAttrgroupRelationDao;
import com.youbo.cloudmall.product.dao.CategoryDao;
import com.youbo.cloudmall.product.entity.AttrAttrgroupRelationEntity;
import com.youbo.cloudmall.product.entity.AttrEntity;
import com.youbo.cloudmall.product.entity.CategoryEntity;
import com.youbo.cloudmall.product.service.AttrService;
import com.youbo.cloudmall.product.vo.AttrGroupWithAttrsVo;
import com.youbo.common.constant.ProductConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youbo.common.utils.PageUtils;
import com.youbo.common.utils.Query;

import com.youbo.cloudmall.product.dao.AttrGroupDao;
import com.youbo.cloudmall.product.entity.AttrGroupEntity;
import com.youbo.cloudmall.product.service.AttrGroupService;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Resource
    private AttrService attrService;

    @Autowired
    private CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (catelogId == 0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }
    }


    //获取当前分组没有关联的所有属性
    @Override
    public PageUtils getNoRelationAttr(Map<String, Object> params, Long attrgroupId) {
        //1、当前分组只能关联自己所属分类里面的属性
        AttrGroupEntity attrGroupEntity = baseMapper.selectById(attrgroupId);
        CategoryEntity categoryEntity = null;
        PageUtils pageUtils = null;
        if (attrGroupEntity != null) {
            Long catelogId = attrGroupEntity.getCatelogId();
            categoryEntity = categoryDao.selectById(catelogId);
            //2、当前分组只能关联别的分组没有引用的属性
            List<AttrGroupEntity> group = baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId).ne("attr_group_id",attrgroupId));
            List<Long> list = group.stream().map(item -> {
                return item.getAttrGroupId();
            }).collect(Collectors.toList());
            QueryWrapper<AttrAttrgroupRelationEntity> wrapper1 =new QueryWrapper<>();
            if (list != null && list.size() > 0) wrapper1.in("attr_group_id",list);
            List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationDao.selectList(wrapper1);
            List<Long> attrIds = relationEntities.stream().map(item -> {
                return item.getAttrId();
            }).collect(Collectors.toList());
            QueryWrapper<AttrEntity> wrapper2 = new QueryWrapper<AttrEntity>().eq("catelog_id",attrGroupEntity.getCatelogId());
            if (attrIds != null && attrIds.size()>0) wrapper2.notIn("attr_id",attrIds);
            String key = (String) params.get("key");
            if (!StringUtils.isEmpty(key)) {
                wrapper2.and(w->{
                    w.eq("attr_id",key).or().like("attr_name",key);
                });
            }
            IPage<AttrEntity> page = attrService.page(new Query<AttrEntity>().getPage(params), wrapper2);
            pageUtils = new PageUtils(page);
        }

        return pageUtils;
    }

    //根据分类ID查出所有的分组以及分组里的属性
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByActelogId(Long catelogId) {
        //查询分组信息。
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        //查询所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group,attrsVo);
            Long attrGroupId = group.getAttrGroupId();
            List<AttrEntity> relationAttr = attrService.getRelationAttr(attrGroupId);
            attrsVo.setAttrs(relationAttr);
            return attrsVo;
        }).collect(Collectors.toList());
        return collect;
    }
}

